<template>
  <div>
    <!-- 下拉列表 -->
    <el-row type="flex" justify="end">
      <!-- 年份 -->
      <el-select
        size="small"
        placeholder="请选择年份"
        v-model="year"
        @change="dateChange"
      >
        <el-option
          v-for="item in yearsList"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <!-- 月份 -->
      <el-select
        size="small"
        placeholder="请选择月份"
        v-model="month"
        @change="dateChange"
      >
        <el-option
          v-for="item in 12"
          :key="item"
          :value="item"
          :label="item"
        ></el-option>
      </el-select>
    </el-row>
    <!-- 日历 -->
    <el-calendar v-model="curDate" v-if="isShow">
      <!-- 
			dateCell 插槽名 
			date：单元格代表的日期
			data：{ type， isSelected， day}，
			type， 表示该日期的所属月份，可选值有 prev-month，current-month，next-month; 
			isSelected：标明该日期是否被选中; 
			day：是格式化的日期，格式为 yyyy-MM-ddtypeisSelectedday
			-->
      <template v-slot:dateCell="{ data, date }">
        {{ data.day | formatDay }}
        <span v-if="isWeek(date)" class="week">休</span>
      </template>
    </el-calendar>
  </div>
</template>


<script>
const curDate = new Date() // 当前时间
const year = curDate.getFullYear()  // 当前年份
const month = curDate.getMonth() + 1 // 当前月份
const yearsList = [] // 保存年份列表
for (let i = 0; i < 11; i++) {
  yearsList.push(year - 5 + i)
}

export default {
  // 时间过滤器
  filters: {
    // 格式化日历时间
    formatDay (val) {
      return Number(val.split('-')[2])
    }
  },
  data () {
    return {
      curDate,
      yearsList,
      year,
      month,
      isShow: true
    }
  },
  watch: {
    // 日历关联下拉列表
    curDate (newval) {
      // console.log(newval);
      this.year = newval.getFullYear()
      this.month = newval.getMonth() + 1
    },
    // 日历语言实时更换
    '$i18n.locale' (val) {
      // 当语言变化时修改 isShow为false  
      this.isShow = false
      // Dom渲染之后 isShow 变为 true  
      this.$nextTick(() => {
        this.isShow = true
      })
    }
  },
  methods: {
    // 下拉列表关联日历
    dateChange () {
      this.curDate = new Date(this.year + '-' + this.month)
    },
    isWeek (date) {
      // 当getday 方法返回0或6的时候 这个日期对应的是在周六或周日
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style  scoped>
/deep/.el-calendar-day {
  position: relative;
}
.week {
  font-size: 12px;
  position: absolute;
  top: 9px;
  right: 16px;
  display: inline-block;
  width: 1.5em;
  height: 1.5em;
  text-align: center;
  line-height: 1.5em;
  border-radius: 50%;
  background: #fa7c4d;
  color: #fff;
}
/deep/ .el-calendar-day {
  height: auto;
}
/deep/ .el-calendar-table__row td,
/deep/ .el-calendar-table tr td:first-child,
/deep/ .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
/deep/ .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
/deep/ .el-calendar__header {
  display: none;
}
</style>